import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'

# Code Interpreter

The Code Interpreter in Flappy serves as an equivalent replacement for [ChatGPT Code Interpreter](https://openai.com/blog/chatgpt-plugins#code-interpreter), enabling the language model to fulfill complex user requirements through Python code. What sets Flappy's Code Interpreter apart from other open-source implementations in the market is its sandboxed safety feature. This ensures that it meets the stringent security needs necessary for deployment in a production environment.

## How it works

Flappy's Code Interpreter is built with Rust, featuring a WebAssembly runtime environment that complies with the [WASIX specifications](https://wasix.org/docs/api-reference). Unlike standard WebAssembly runtimes, this environment hosts a Python interpreter and supports nearly all POSIX interfaces, including socket, net, and filesystem IO operations. This means that even though the Python interpreter operates within a wasm sandbox, all functionalities remain fully intact.

The current version of the Python interpreter used in this environment is `Python 3.12`. All standard libraries (stdlib) work as expected. We are also in the process of adding support for the pip package manager, which will allow for further extension of capabilities.

## Security-First Design

Our core design principle is "safety first". By default, access to the external network is off to ensure a secure environment. Similarly, environment variables are sandbox-isolated. However, if required, these settings can be overridden using custom configurations.

## Usage

<Tabs>
  <TabItem value="nodejs" label="NodeJS (TypeScript)" default>

```ts
import { createFlappyAgent, ChatGPT, createCodeInterpreter } from '@pleisto/node-flappy'
import OpenAI from 'openai'

const gpt35 = new ChatGPT(
  new OpenAI({
    apiKey: process.env.OPENAI_API_KEY!,
    baseURL: process.env.OPENAI_API_BASE!
  })
)

const agent = createFlappyAgent({
  llm: gpt35,
  features: [createCodeInterpreter({}, { enableNetwork: false })]
})

void agent.executePlan(
  'There are some rabbits and chickens in a barn. What is the number of chickens if there are 396 legs  and 150 heads in the barn?'
)
```

  </TabItem>
  <TabItem value="java" label="Java" default>

```java
public class CodeInterpreter {

  public static void main(String[] args) throws ExecutionException, InterruptedException {
    Dotenv dotenv = Dotenv.load();
    ChatGPT llm = new ChatGPT(new ChatGPT.ChatGPTConfig(null, dotenv.get("OPENAI_TOKEN"), dotenv.get("OPENAI_API_BASE")));

    FlappyBaseAgent agent = new FlappyBaseAgent(
      llm, Collections.singletonList(new FlappyCodeInterpreter())
    );

    Future<String> future = agent.executePlanAsync("There are some rabbits and chickens in a barn. What is the number of chickens if there are 396 legs and 150 heads in the barn?");
    String ret = future.get();
  }
}
```


  </TabItem>
  <TabItem value="kotlin" label="Kotlin" default>

```kotlin
  val dotenv = dotenv()
  val chatGPT = ChatGPT(
    ChatGPT.ChatGPTConfig(token = dotenv["OPENAI_TOKEN"], host = dotenv["OPENAI_API_BASE"])
  )

  val agent = FlappyBaseAgent(
    maxRetry = 2,
    inferenceLLM = chatGPT,
    features = listOf(FlappyCodeInterpreter())
  )

  agent.use {
    it.executePlan<String>("There are some rabbits and chickens in a barn. What is the number of chickens if there are 396 legs and 150 heads in the barn?")
  }
```

  </TabItem>
  <TabItem value="csharp" label="C#" default>

```csharp
var gpt35 = new ChatGPT(new OpenAIAPI
{
  Auth = new APIAuthentication(apiKey: OpenAIApiKey),
  ApiUrlFormat = Environment.GetEnvironmentVariable("OPENAI_API_URL"),
  ApiVersion = "v1",
}, "gpt-3.5-turbo", null, Logger.CreateLogger<ChatGPT>());

var agent = new FlappyAgent(new FlappyAgentConfig
{
  LLM = gpt35,
  Features = new IFlappyFeature[]
   {
     new CodeInterpreterFeature(null,new CodeInterpreterOptions
     {
        EnableNetwork=false,
     })
   }
}, null, null, Logger.CreateLogger<FlappyAgent>());
var data = await agent.ExecutePlan("There are some rabbits and chickens in a barn. What is the number of chickens if there are 396 legs and 150 heads in the barn?");
Console.WriteLine($"====================== Final Result =========================");
Console.WriteLine(data.ToString());
Console.WriteLine($"====================== Final Result Of Data =========================");
```

  </TabItem>
</Tabs>
